<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>建筑公司管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../assets/layui/css/layui.css" media="all">
</head>
<body>
<div style="padding: 10px;">
    <from class="layui-form" action="">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label" style="width: auto;">施工单位名称</label>
                <div class="layui-input-inline" style="width: 200px;">
                    <input type="text" id="name" name="name" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <button class="layui-btn layui-btn-normal" data-type="search" id="btnSearch">查询</button>
                <button class="layui-btn layui-btn-primary" data-type="doAdd" id="btnAdd">添加</button>
                <!--<button class="layui-btn" data-type="doImport" id="btnImport" >导入</button>-->
            </div>
        </div>
    </from>
</div>
<table class="layui-table" lay-filter="tbFliter" id="tbData" style="margin-top: -25px;">
</table>
<script src="../assets/layui/layui.js"></script>
<script>
    layui.config({
        base: '../js/'
    });
    layui.use(['jquery','table','common','laydate'], function () {
        var table = layui.table,
            common=layui.common,
            laydate=layui.laydate,
            $ = layui.jquery,
            active = {
                search: function () {
                    table.reload("tbOptions", {
                        where: {
                            name: $("#name").val()
                        },page:{
                            curr: 1
                        }
                    });
                },
                doAdd: function () {
                    common.layer_show("施工单位新增",'builderAdd.html?type=add');
                },
                doImport: function () {
                    common.layer_show("企业导入",'companyImport.html',"400","300");
                }
            };
        var op=common.getUrlParam("op");

        //表格初始化
        table.render({
            elem: '#tbData'
            , skin: 'row'
            , url: 'list?span='+new Date().Format("yyyyMMddHHmmss")
            , page: true
            , cols: [[
                {field: 'builderName', title: '施工单位名称'}
                , {field: 'licno', title: '营业执照号'}
                , {field: 'userName', title: '责任人'}
                , {field: 'phone', title: '手机号/企业账号'}
                , {field: 'operation', title: '操作', width: 400,templet: function(d){
                        var html='<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="view">查看</a>';
                        html+='<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>';
                        html+=' <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="resetpwd">密码重置</a>';
                        if(d.status=='0'){
                            html += '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
                        }else {
                            html+= '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="qiyong">启用</a>';
                        }
                        return html;
                    }}
            ]]
            , limits: [10, 20, 50]
            , where: {
                name: $("#name").val()
            }
            , id: 'tbOptions'
        });

        table.on('tool(tbFliter)', function (obj) {
            var data = obj.data; //获得当前行数据
            var id = data.id;
            var phone=data.phone;
            var layEvent = obj.event; //获得 lay-event
            if (layEvent === 'view') { //编辑
                common.layer_show('企业详情',"builderDetail.html?id="+id);
            }
            else if (layEvent === 'edit') { //编辑
                common.layer_show('企业编辑',"builderAdd.html?type=edit&id="+id);
            } else if (layEvent === 'del'||layEvent === 'qiyong') { //删除
                // if(op!="2"){
                //     common.showError("仅能由项目管理员进行施工单位的增删");
                //     return false;
                // }
                var str="删除";
                if (layEvent === 'qiyong'){
                    str='启用';
                }
                layer.confirm('您确定'+str+'该施工单位吗？', function (index) {
                    $.ajax({
                        type: 'POST',
                        url: 'delete',
                        dataType: 'json',
                        data: {id: id,phone:phone,status:layEvent},
                        success: function (rtn) {
                            if (rtn.code == 0) {
                                common.showMsg('操作成功!');
                                table.reload("tbOptions", {
                                    where: {
                                        name: $("#name").val()
                                    },page:{
                                        curr: 1
                                    }
                                });
                            }
                            else {
                                common.showError("操作失败:" + rtn.msg);
                            }
                        },
                        error: function () {
                            common.showError("操作失败");
                        }
                    });

                });
            }else if(layEvent==='resetpwd') {
                layer.confirm('您确定重置该用户的密码吗？', function (index) {
                    $.ajax({
                        type: 'POST',
                        url: 'pwdResert',
                        dataType: 'json',
                        data: {phone: phone},
                        success: function (rtn) {
                            if (rtn.code === 0) {
                                common.showInfo("操作成功,已重置为系统默认密码:" + rtn.data);
                            }
                            else {
                                common.showError("操作失败:" + rtn.msg);
                            }
                        },
                        error: function () {
                            common.showError("操作失败");
                        }
                    });
                });
            }
        });


        //按钮事件绑定
        $('.layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>